<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author QUYET
 */
class Admin extends Admin_Controller {
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('news_m');
        $this->load->library('pagination');
        $this->load->library('form_validation');        
        $this->session->set_userdata('current_url', current_url());
        
    }
    function index(){
        //$this->session->set_userdata('current_url', current_url());
        //$all_news = $this->news_m->get_allnews();
        $this->_data['module_title'] = __('admin_news_list');
        $per_page = 10;
        $config['full_tag_open'] = "<div class='page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = __('admin_next');
        $config['prev_link'] = __('admin_previous');
        $config['first_link'] = __('admin_first');
        $config['last_link'] = __('admin_last');
        $config['base_url'] = base_url() . 'admin/news/';
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 3;
        $config['total_rows'] = $this->news_m->count_all_news();
        $off_set = $this->uri->segment(3) != '' ? $this->uri->segment(3) : 0;
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['allnews'] = $this->news_m->get_allnews($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['off_set'] = $off_set;
        
        $this->display_admin('admin/news',$this->_data);
    }
    
    function add() {
        $this->_data['module_title'] = __('admin_news_add');
        if (isset($_POST['btnSubmit'])) {
            $this->form_validation->set_rules('title', __('admin_aboutus_title'), 'trim|required|max_length[300]|xss_clean');
            $this->form_validation->set_rules('description', __('admin_aboutus_description'), 'trim|required|xss_clean');
            $this->form_validation->set_rules('meta_title', __('admin_aboutus_meta_title'), 'trim|required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('meta_keyword', __('admin_aboutus_meta_keyword'), 'trim|required|max_length[400]|xss_clean');
            $this->form_validation->set_rules('meta_description', __('admin_aboutus_meta_description'), 'trim|required|max_length[400]|xss_clean');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run()) {                
                    $news_data = array();
                    $news_data['title'] = $_POST['title'];
                    $news_data['description'] = $_POST['description'];
                    $news_data['meta_title'] = $_POST['meta_title'];
                    $news_data['meta_keyword'] = $_POST['meta_keyword'];
                    $news_data['meta_description'] = $_POST['meta_description'];
                    if ($this->news_m->add_news($news_data)) {
                        redirect('admin/news');
                    } else {
                        $this->_data['errors'] = __('admin_new_error_add');
                        $this->display_admin('admin/add');
                    }
            } else {
                $this->_data['errors'] = validation_errors();
                $this->display_admin('admin/add');
            }
        } else {
            $this->display_admin('admin/add');
        }
    }

    function edit($id_new) {
        $this->_data['module_title'] = __('admin_news_edits');
        if(!is_numeric($id_new)) redirect('admin/news');
        $news = $this->news_m->get_onenews($id_new);
        if (!is_object($news)) {
            redirect('admin/news');
        }
        $this->_data['one_news'] = $news;
        if (isset($_POST['btnEdit'])) {
            $this->form_validation->set_rules('title', __('admin_aboutus_title'), 'trim|required|max_length[300]|xss_clean');
            $this->form_validation->set_rules('description', __('admin_aboutus_description'), 'trim|required|xss_clean');
            $this->form_validation->set_rules('meta_title', __('admin_aboutus_meta_title'), 'trim|required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('meta_keyword', __('admin_aboutus_meta_keyword'), 'trim|required|max_length[400]|xss_clean');
            $this->form_validation->set_rules('meta_description', __('admin_aboutus_meta_description'), 'trim|required|max_length[400]|xss_clean');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run()) {                
                    $news_data = array();
                    $news_data['title'] = $_POST['title'];
                    $news_data['description'] = $_POST['description'];
                    $news_data['meta_title'] = $_POST['meta_title'];
                    $news_data['meta_keyword'] = $_POST['meta_keyword'];
                    $news_data['meta_description'] = $_POST['meta_description'];
                    if ($this->news_m->update_news($news_data,$id_new)) {
                        redirect('admin/news');
                    } else {
                        $this->_data['errors'] = __('admin_new_error_add');
                        $this->display_admin('admin/edit/'.$id_new);
                    }
            } else {
                $this->_data['errors'] = validation_errors();
                $this->display_admin('admin/add');
            }
        } else {
            if(!empty($news)){
                $_POST['title'] = $news->title;
                $_POST['description'] = $news->description;
                $_POST['meta_title'] = $news->meta_title;
                $_POST['meta_keyword'] = $news->meta_keyword;
                $_POST['meta_description'] = $news->meta_description;
            }
            
            $this->display_admin('admin/edit',$this->_data);
        }
    }
    
    function delete($id_news=1){
        if(!is_numeric($id_news)) redirect('admin/news');
        $news = $this->news_m->get_onenews($id_news);
        if (!is_object($news)) {
            redirect('admin/news');
        }
        $this->db->delete('news',array('id'=>$id_news));
        redirect(base_url().'admin/news/');
    }
}

?>
